package com.wework.api;

import com.wework.utils.FakerUtils;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class DepartmentObject {

    public static Response createDept(String createName,String createEnName){
        String body="{\n" +
                "   \"name\": \""+createName+"\",\n" +
                "   \"name_en\": \""+createEnName+"\",\n" +
                "   \"parentid\": 1\n"+
                "}\n";

        final Response response=given().filter(new TokenFilter()).log().all()
                .contentType("application/json")
                .body(body)
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/create")
                .then().log().body()
                .extract().response();

        return response;
    }

    public static Response createDept(){
        String createName="name"+ FakerUtils.getTimeStamp();
        String createEnName="en_name"+FakerUtils.getTimeStamp();
        String body="{\n" +
                "   \"name\": \""+createName+"\",\n" +
                "   \"name_en\": \""+createEnName+"\",\n" +
                "   \"parentid\": 1\n"+
                "}\n";

        final Response response=given().filter(new TokenFilter()).log().all()
                .contentType("application/json")
                .body(body)
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/create")
                .then().log().body()
                .extract().response();

        return response;
    }

    public static Response updateDept(String id,String name,String enName){
        String body="{\n" +
                "   \"id\": "+id+",\n" +
                "   \"name\": \""+name+"\",\n" +
                "   \"name_en\": \""+enName+"\",\n" +
                "   \"parentid\": 1\n" +
                "}";
        Response response=given().filter(new TokenFilter()).log().all()
                .contentType("application/json")
                .when()
                .body(body)
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/update")
                .then()
                .log().body()
                .extract().response();

        return response;
    }

    public static Response deleteDept(String deptId){
        Response response=given().filter(new TokenFilter()).log().all()
                .contentType("application/json")
                .when()
                .param("id",deptId)
                .get("https://qyapi.weixin.qq.com/cgi-bin/department/delete")
                .then()
                .log().body()
                .extract().response();

        return response;
    }

    public static Response listDept(String deptId){

        Response response=given().filter(new TokenFilter()).log().all()
                .when()
                .param("id",deptId)
                .get("https://qyapi.weixin.qq.com/cgi-bin/department/list")
                .then()
                .log().body()
                .extract().response();
        return response;
    }
}
